﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace SaveFuel.NumberSelector
{
    public partial class MoneySpent : PhoneApplicationPage
    {
        public MoneySpent()
        {
            InitializeComponent();

            InitializeSymbol();
            LoadDataBack();
        }

        private void LoadDataBack()
        {
            if (App.money != string.Empty)
            {
                string[] moneySpentArray = App.money.Split(Convert.ToChar(App.currencySeparator));
                string value = moneySpentArray[0];
                string decimals = moneySpentArray[1];

                int[] money = new int[3];
                int[] cents = new int[2];

                for (int i = 0; i < money.Length; i++)
                {
                    money[i] = Convert.ToInt32(value[i].ToString());
                }

                for (int i = 0; i < decimals.Length; i++)
                {
                    cents[i] = Convert.ToInt32(decimals[i].ToString());
                }

                WriteDataToLoopingSelector(money, cents);
            }
        }

        private void WriteDataToLoopingSelector(int[] money, int[] cents)
        {
            this.Cell1.DataSource.SelectedItem = money[0];
            this.Cell2.DataSource.SelectedItem = money[1];
            this.Cell3.DataSource.SelectedItem = money[2];

            this.Cell4.DataSource.SelectedItem = cents[0];
            this.Cell5.DataSource.SelectedItem = cents[1];
        }

        private void InitializeSymbol()
        {
            this.currencySeparator.Text = App.currencySeparator;
        }

        private void doneClick_Click(object sender, EventArgs e)
        {
            string result = this.Cell1.DataSource.SelectedItem.ToString() + this.Cell2.DataSource.SelectedItem.ToString() + this.Cell3.DataSource.SelectedItem.ToString() + 
                            App.currencySeparator + 
                            this.Cell4.DataSource.SelectedItem.ToString() + this.Cell5.DataSource.SelectedItem.ToString();

            App.money = result;

            NavigationService.GoBack();
        }

        private void cancelClick_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}